const express = require('express');
const router = express.Router();

const { no, ok } = require('../../utils/utlis');
const svgCaptcha = require('svg-captcha');
const { getKey, setKey, delKey } = require('../../utils/redis');
const { v4: uuidv4 } = require('uuid');

router.post('/', async (req, res, next) => {
	try {
		let svg = svgCaptcha.createMathExpr({
			size: 4,
			ignoreChars: '0o1i',
			noise: 3, //这防线
			color: true,
			background: '#f0b27a',
			width: 120,
			height: 40,
			fontSize: 40
		});

		let uuid = uuidv4();
		const data = {
			captchaKey: uuid,
			svg: svg.data
		};
		// 存入redis 验证码
		await setKey(uuid, svg.text, 120);
		res.send(ok(data));
	} catch (error) {
		res.send(no(error.message));
	}
});

module.exports = router;
